package com.hilton.tigershark.view;

import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.hilton.tigershark.Tab;
import com.hilton.tigershark.TabControl;

public class ErrorConsoleManager {
    private final TabControl mTabControl;
    private final LinearLayout mErrorConsoleContainer;
    private boolean mShouldShowErrorConsole;
    
    public ErrorConsoleManager(TabControl tc, LinearLayout container) {
	mTabControl = tc;
	mErrorConsoleContainer = container;
	mShouldShowErrorConsole = false;
    }
    
    public void showErrorConsole() {
	if (mShouldShowErrorConsole) {
	    return;
	}
	mShouldShowErrorConsole = true;
	ErrorConsoleView errorConsole = getCurrentErrorConsole();
	if (errorConsole == null) {
	    return;
	}
	attachErrorConsole(errorConsole);
    }
    
    public void attachErrorConsole(ErrorConsoleView errorConsole) {
	if (errorConsole.numberOfErrors() > 0) {
	    errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
	} else {
	    errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
	}
	mErrorConsoleContainer.addView(errorConsole, new LinearLayout.LayoutParams(
			ViewGroup.LayoutParams.MATCH_PARENT,
			ViewGroup.LayoutParams.WRAP_CONTENT));
    }
    private ErrorConsoleView getCurrentErrorConsole() {
	final Tab t = mTabControl.getCurrentTab();
	if (t == null) {
	    return null;
	}
	return t.getErrorConsole();
    }
    
    public void hideErrorConsole() {
	mShouldShowErrorConsole = false;
	final ErrorConsoleView console = getCurrentErrorConsole();
	detachErrorConsole(console);
    }

    public void detachErrorConsole(final ErrorConsoleView console) {
	if (console != null) {
	    mErrorConsoleContainer.removeView(console);
	}
    }
    
    
    public boolean shouldShowErrorConsole() {
        return mShouldShowErrorConsole;
    }
}
